package org.glut.service;

import org.glut.bean.CFAir;
import org.glut.serialport.manager.SerialPortManager;
import org.glut.serialport.ui.MainFrame;
import org.glut.util.GetApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Scope("singleton")
public class CFAirService {
    @Autowired
    MessageService messageService;
    //    @Autowired
//    private  static MainFrame mainFrame;
    @Autowired
    public static CFAir cfAir1;
    public static CFAir cfAir2;
    public List<String> cftimeList1 = new ArrayList<>();
    public List<String> cfnumList1 = new ArrayList<>();
    public List<String> cfpower1 = new ArrayList<>();
    public List<String> cftimeList2 = new ArrayList<>();
    public List<String> cfnumList2 = new ArrayList<>();
    public List<String> cfpower2 = new ArrayList<>();
    private SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");//设置时间

    public CFAirService() {
        cfAir1 = new CFAir();
        cfAir2 = new CFAir();
//        (CFAir)GetApplicationContext.getBean("CFAir")
    }


    public void getmsgs(String[] message) {
//        for (String s : message) {
//            System.out.println(s);
//        }
//        cfAir = (CFAir) GetApplicationContext.getBean("CFAir");
        char id = message[0].charAt(2);
        if (id == '1') {
            initCF(cfAir1,message,cftimeList1,cfnumList1,cfpower1);
        } else if (id == '2') {
            initCF(cfAir2,message,cftimeList2,cfnumList2,cfpower2);
        }


//            System.out.println(timeList);
//            System.out.println(cfList);
    }

    private void initCF(CFAir cfAir, String[] message, List<String> cftimeList, List<String> cfnumList, List<String> cfpower) {
//        CF2 B3548 1 P001 T020 V1E
        cfAir.setId(message[0].charAt(2)+"");
        cfAir.setPower(message[1].substring(1));
        cfAir.setRank(message[2]);
        cfAir.setCurNum(message[3].substring(1));
        cfAir.setMax(message[4].substring(1));
        cfAir.setStatus(message[5].substring(1));
        String time = df.format(new Date());
        cftimeList.add(time);
        cfnumList.add(cfAir.getCurNum());
        cfpower.add(cfAir.getPower());
    }

    /**
     * 发送命令控制厨房燃气
     * @param num
     * @param auto
     * @param status
     */
    public void setCmd(String num, String auto, String status,String id) {
        String cmd = "CF"+id+"A1M" + auto + "T" + num + "V" + status;

        System.out.println(cmd);
        if (messageService.outputStream != null) {
            try {
                messageService.outputStream.write(cmd.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println(cmd);
//            System.out.println("------socket未连接，outputStream为空------");
        }
//        mainFrame = (MainFrame) GetApplicationContext.getBean("MainFrame");

        try {
            if (MainFrame.mSerialport != null) {
                SerialPortManager.sendToPort(MainFrame.mSerialport, cmd.getBytes());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
